package com.litblue.artwork.listener;

import com.litblue.artwork.mapper.LitArtworkUserMapper;
import com.litblue.starter.mq.MqExChangeArtWorkInterProperties;
import com.litblue.starter.pojo.artwork.dto.LitArtworkInfoDto;
import com.litblue.starter.pojo.artwork.dto.LitArtworkUserDto;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
@Slf4j
@AllArgsConstructor
public class ArtworkLikeStatusListener {

    private final LitArtworkUserMapper litArtworkUserMapper;
    // 监听RabbitMQ的指定队列，当有消息到达时，执行此方法
    // 队列名为MqExChangeArtWorkInterProperties.QUEUE_ARTWORK_ADD，交换机为MqExChangeArtWorkInterProperties.DIRECT_ARTWORK，路由键为MqExChangeArtWorkInterProperties.ROUTING_ARTWORK_KEY_ADD
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MqExChangeArtWorkInterProperties.QUEUE_ARTWORK_ADD),
            exchange = @Exchange(name = MqExChangeArtWorkInterProperties.DIRECT_ARTWORK,type= ExchangeTypes.DIRECT),
            key = MqExChangeArtWorkInterProperties.ROUTING_ARTWORK_KEY_ADD
    ))
    // 插入艺术品点赞信息到数据库
    // 参数：litArtworkUserDto 包含艺术品和用户信息的DTO对象
    public void listenerArtworkLike(LitArtworkUserDto litArtworkUserDto){
        litArtworkUserMapper.insert(litArtworkUserDto);
    }
    // 监听RabbitMQ的队列，用于处理艺术作品删除事件
    // 队列、交换机和路由键的名称均来自配置类MqExChangeArtWorkInterProperties
    // 该监听器使用DIRECT类型的交换机，确保消息路由的精确性
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MqExChangeArtWorkInterProperties.QUEUE_ARTWORK_DEL),
            exchange = @Exchange(name = MqExChangeArtWorkInterProperties.DIRECT_ARTWORK,type= ExchangeTypes.DIRECT),
            key = MqExChangeArtWorkInterProperties.ROUTING_ARTWORK_KEY_REMOVE
    ))
    public void listenerArtworkLikeRemove(LitArtworkUserDto litArtworkUserDto){
        // 当接收到艺术作品删除事件时，调用数据库操作方法removeArtwork删除指定的艺术作品信息
        litArtworkUserMapper.removeArtwork(litArtworkUserDto);
    }
}
